#!/bin/bash

#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=2
#SBATCH --gres=gpu:0
#SBATCH --mem=4G
#SBATCH --partition=cpu
#SBATCH --qos=cpu_qos
#SBATCH --job-name=esn_5eetV
#SBATCH --output=%j_%x.out
#SBATCH --error=%j_%x.err
#SBATCH --time=08:00:00


CONFIG_PATH=$1
EXPERIMENT_DIRECTORY=$2
HIDDEN_DIM=$3
ALPHA_VALUE=$4
GAMMA_VALUE=$5
SIGMA_VALUE=$6
K_VALUE=$7
ETA_VALUE=$8
CLIENT_T_VALUE=${9}
GAME_SYNC_VALUE=${10}
GAME_T_VALUE=${11}
VENV_PATH=${12}

RUN_NAMES=( "Run1" "Run2" "Run3" )
SEEDS=(2024 2025 2026)


echo "Python Venv Path: ${VENV_PATH}"
echo "World size: ${SLURM_NTASKS}"
echo "Number of nodes: ${SLURM_NNODES}"


# Source the environment
source ${VENV_PATH}bin/activate
echo "Active Environment:"
which python



for ((i=0; i<${#RUN_NAMES[@]}; i++));
do
    RUN_NAME="${RUN_NAMES[i]}"
    SEED="${SEEDS[i]}"
    # create the run directory
    RUN_OUTPUT_DIR="${EXPERIMENT_DIRECTORY}${RUN_NAME}/"

    echo "Starting Run and logging artifcats at ${RUN_OUTPUT_DIR}"
    if [ -d "${RUN_OUTPUT_DIR}" ]
    then
        # Directory already exists, we check if the done.out file exists
        if [ -f "${RUN_OUTPUT_DIR}done.out" ]
        then
            # Done file already exists so we skip this run
            echo "Run already completed. Skipping Run."
            continue
        else
            # Done file doesn't exists (assume pre-emption happened)
            # Delete the partially finished contents and start over
            echo "Run did not finished correctly. Re-running."
            rm -r "${RUN_OUTPUT_DIR}"
            mkdir "${RUN_OUTPUT_DIR}"
        fi
    else
        # Directory doesn't exist yet, so we create it.
        echo "Run directory does not exist. Creating it."
        mkdir "${RUN_OUTPUT_DIR}"
    fi

    RUN_OUTPUT_FILE="${RUN_OUTPUT_DIR}log.out"

    python -m experiments.esn_experiments.run_esn_experiment \
        --config_path ${CONFIG_PATH} \
        --result_dir ${RUN_OUTPUT_DIR} \
        --hidden_dim ${HIDDEN_DIM} \
        --alpha ${ALPHA_VALUE} \
        --gamma ${GAMMA_VALUE} \
        --sigma ${SIGMA_VALUE} \
        --K ${K_VALUE} \
        --eta ${ETA_VALUE} \
        --client_T ${CLIENT_T_VALUE} \
        --game_sync_freq ${GAME_SYNC_VALUE} \
        --game_T ${GAME_T_VALUE} \
        --random_seed ${SEED} \
        > ${RUN_OUTPUT_FILE} 2>&1 &

        wait
        # Create a file that verifies that the Run concluded properly
        touch "${RUN_OUTPUT_DIR}done.out"
done
